#!/usr/bin/env python
# -*- coding:utf-8 -*-

from flask import Flask, render_template, request, redirect, session

app = Flask(__name__)  # flask 对象

shengjilis = {
    'jilin': {'长春市': '220100', '吉林市': '220200', '四平市': '220300', '辽源市': '220400', '通化市': '220500', '白山市': '220600',
              '松原市': '220700', '白城市': '220800'},

    'heilongjiang': {'哈尔滨市': '230100', '齐齐哈尔市': '230200', '鸡西市': '230300', '鹤岗市': '230400', '双鸭山市': '230500',
                     '大庆市': '230600', '伊春市': '230700', '佳木斯市': '230800', '七台河市': '230900', '牡丹江市': '231000',
                     '黑河市': '231100', '绥化市': '231200'},

    'fujian': {'厦门市': '350200', '莆田市': '350300', '三明市': '350400', '泉州市': '350500', '漳州市': '350600', '南平市': '350700',
               '龙岩市': '350800', '宁德市': '350900'},

    'guangdong': {'广州市': '440100', '韶关市': '440200', '深圳市': '440300', '珠海市': '440400', '汕头市': '440500', '佛山市': '440600',
                  '江门市': '440700', '湛江市': '440800', '茂名市': '440900', '肇庆市': '441200', '惠州市': '441300', '梅州市': '441400',
                  '汕尾市': '441500', '河源市': '441600', '阳江市': '441700', '清远市': '441800', '东莞市': '441900', '中山市': '442000',
                  '潮州市': '445100', '揭阳市': '445200', '云浮市': '445300'}

}
app.config.from_object("settings.Config")  # 配置文件存放


# 登录
@app.route('/login', methods=['GET', 'POST'])
def login():
    if request.method == 'GET':
        return render_template('login.html')
    user = request.form.get('user')
    pwd = request.form.get('pwd')

    if user == 'aries' and pwd == '123456':

        session['user_info'] = user
        return render_template('success.html')
    elif user == 'tomato' and pwd == '666666':

        session['user_info'] = user
        return render_template('success.html')
    elif user == 'youge' and pwd == '555555':

        session['user_info'] = user
        return render_template('success.html')
    else:
        return render_template('fail.html')


@app.route('/ip_query')
def ip_query():
    userinfo = session.get('user_info')
    if not userinfo:
        return redirect('/login')
    from ipdw import ipdw
    city_name = request.args.get('name')
    if not city_name:
        return render_template('ip_query.html')
    data = ipdw(city_name)
    print(data)  #
    if not data:
        return render_template('ip_query.html', msg='输入有误，请重试')
    return render_template('ip_query.html', jg=data)


@app.route('/weather/<username>')
def weather(username):
    from cs import cscx

    userinfo = session.get('user_info')
    if not userinfo:
        return redirect('/login')

    a1 = shengjilis.get(str(username))

    lis = []
    for k in a1.keys():
        lis.append(k)

    city_name = request.args.get('city')

    if not city_name:
        data_list = cscx(a1[lis[0]])
        today = data_list[0]
        future = data_list[1:]
        return render_template('weather.html', city_name=lis[0], future=future, today=today, city=lis,
                               username=username)

    city = a1.get(city_name)

    data_list = cscx(city)
    today = data_list[0]
    future = data_list[1:]
    return render_template('weather.html', city_name=city_name, future=future, today=today, city=lis, username=username)


@app.route('/index')
def index():
    userinfo = session.get('user_info')
    if not userinfo:
        return redirect('/login')

    return render_template('index.html')


@app.route('/address')
def address():
    userinfo = session.get('user_info')
    if not userinfo:
        return redirect('/login')
    from jwd import baidu
    city_name = request.args.get('name')
    if not city_name:
        return render_template('address.html')
    data = baidu(city_name)
    print(data)  #
    if not data:
        return render_template('address.html', msg='输入有误，请重试')
    return render_template('address.html', jg=data)


@app.route('/administrative')
def administrative():
    userinfo = session.get('user_info')
    if not userinfo:
        return redirect('/login')

    city_name = request.args.get('name')
    if not city_name:
        return render_template('administrative.html')
    from xzqycx import xzq
    qjdz = xzq(city_name)
    if not qjdz:
        return render_template('administrative.html', msg='输入有误，请重试')

    return render_template('administrative.html', jg=qjdz)


@app.route('/logout')
def logout():
    del session['user_info']

    return redirect('/login')


@app.route('/')
def logo():
    return redirect('/index')


if __name__ == '__main__':
    app.run()
